package com.chinawayltd.ews.sample.util;

import java.math.BigDecimal;

/**
 * 坐标转换工具
 * 
 * @description
 * @author: chenxin
 * @create: 2018年7月23日 下午1:39:30
 */
public class CoordinateUtil {

	private static double pi = 3.14159265358979324;
	private static double a = 6378245.0;
	private static double ee = 0.00669342162296594323;
	private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

	private static boolean outOfChina(double lat, double lon) {
		if (lon < 72.004 || lon > 137.8347)
			return true;
		if (lat < 0.8293 || lat > 55.8271)
			return true;
		return false;
	}

	private static double transformLat(double x, double y) {
		double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
		ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
		ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
		ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
		return ret;
	}

	/**
	 * 火星坐标转换为百度坐标
	 * 
	 * @param gg_lat,gg_lon
	 *            火星坐标
	 * @param bd_lat,bd_lon百度坐标
	 */
	public static Gps bd_encrypt(double gg_lat, double gg_lon) {
		double bd_lat, bd_lon;
		double x = gg_lon, y = gg_lat;
		double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
		bd_lon = z * Math.cos(theta) + 0.0065;
		bd_lat = z * Math.sin(theta) + 0.006;
		return new Gps(bd_lat, bd_lon);
	}
	public static Gps bdEncrypt(BigDecimal longitude, BigDecimal latitude) {
		double bd_lat, bd_lon;
		double x = longitude.doubleValue(), y = latitude.doubleValue();;
		double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
		double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
		bd_lon = z * Math.cos(theta) + 0.0065;
		bd_lat = z * Math.sin(theta) + 0.006;
		return new Gps(bd_lat, bd_lon);
	}

}
